package com.zsrt.exam.client;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

import com.zsrt.exam.entity.UserEntity;
import com.zsrt.exam.service.UserService;
import com.zsrt.exam.utils.PrintUtil;


/**
 * 系统测试 前提 单元测试必须是OK的。
 * @author zsrt
 *
 */
public class ExamClient {
	
	public static void enterThirdMenu(UserService service,int menu,Scanner sc,List<UserEntity> userList){
		while(true){
			//调出管理员的二级菜单
			service.getMenuAdminSub();
			
			menu = sc.nextInt();
			
			if(menu == 141){
				
				System.out.println("请输入户籍所在地/城市");
				String searchCity = sc.next();
				List<UserEntity> searchList =service.findUsersByCity(searchCity,userList);
				
				int len = searchList.size();
				if(len>0){
					System.err.println("=====================查找成功");
					PrintUtil.printList(searchList);
				}else{
					System.err.println("=====================查无此人");
				}
			}else if(menu == 142){
				
			}else if(menu == 143){
				
			}else if(menu == 144){
				//service.getMenuAdminMain();
				//进入二级菜单
				enterSecondeMenu(service, sc, menu, userList);
				break;
			}
		}
	}
	
	public static void enterFirstMenu(int menu,int counter,Scanner sc,UserService service,List<UserEntity> userList){
		while(true){
			
			String name     = "";
			String password = "";
			if(menu ==1){
				//说明是管理员
				if( counter ==0 ){
					System.out.println("请输入姓名");
					name = sc.next();
					System.out.println("请输入密码");
					password = sc.next();
				}else{
					System.err.println("登陆成功啦.......................");
					break;
				}
				
				
				
				//1 先拿到系统中的管理员
				UserEntity admin = service.getAdmin();
				
				
				if(name.equals(admin.getName()) && password.equals(admin.getPassword())){
					//用户名和密码正确
					//service.getMenuAdminMain();
					counter = counter+1;
					//break;
				}
				
			}else if (menu ==2){
				//普通用户  普通用准考证号码和密码登陆
				
				System.out.println("请输入准考证号码");
				String card  = sc.next();
				System.out.println("请输入密码");
				password = sc.next();
					
					//1 去用户列表中去找看是否有相同的用户 如果有就登陆成功，否则登陆失败
					UserEntity user = service.loginUser(card,password,userList);
					
					if(user != null){
						//调出用户的二级菜单
						enterSecondeMenuUser(service,sc,menu,user,userList);
						//service.getUserMenu2();
						
					}else{
						System.out.println("========登陆失败...");
					}
			}else{
				break;
			}
		}
	}
	
	
	public static void enterSecondeMenuUser(UserService service,Scanner sc,int menu,UserEntity user,List<UserEntity> userList){
		while(true){
			
			service.getUserMenu2();
			menu = sc.nextInt();
			
			if(menu == 22){
				//查询本市的状元 ? 
				//第1个已知的条件： 告诉我当前用户user  ==> city 
				//第2个已知的条件： 告诉我所有的用户列表 userList  ==>根据city查找所有符合条件的列表 zhuangyuanList  sort
				UserEntity zhuangyuan = service.findWinnerOfLocalCity(user,userList);
				
				System.out.println("城市["+user.getCity()+"]的状元郎的具体信息是:"+zhuangyuan);
				
			}else if(menu == 23){
				//查询全国各个省份的状元
				
				List<UserEntity> provinceWinnerList = service.findProvinceWinners(userList);
				
				//System.out.println(provinceWinnerList.size());
				PrintUtil.printList(provinceWinnerList);
				
			}else if(menu == 24){
				//查询指定城市的状元
			}else if(menu == 25){
				//查询各个省份500<=x<=600分的总人数并由高到低排列显示
				
				Map<String, List<UserEntity>> map = service.findAllUsersByProvince(userList);
				
				Set<String> keySet = map.keySet();
				
				Iterator<String> it = keySet.iterator();
				
				while(it.hasNext()){
					
					String province = it.next();
					
					List<UserEntity> provinceUsersList = map.get(province);
					
					System.out.println(province + "\t:\t" + provinceUsersList);
				}
			}
			
			
		}
	}
	
	public static void enterSecondeMenu(UserService service,Scanner sc,int menu,List<UserEntity> userList){
		//2 二级菜单
		while(true){
			
			//调出管理员的二级菜单
			service.getMenuAdminMain();
			
			menu = sc.nextInt();
			
			if(11 == menu){
				//录入考生信息
				//1 增加1个考生  
				System.out.println("请输入考生的姓名...");
				String name = sc.next();
				
				System.out.println("请输入考生的准考证号码...");
				String card = sc.next();
				
				System.out.println("请输入考生的理综的成绩...");
				int other = sc.nextInt();
				
				UserEntity user = service.addUser(name, card, other);
				
				//2 把这个考生加到list
				userList.add(user);
				System.out.println(userList.size());
				PrintUtil.printList(userList);
				System.err.println("=====================录入成绩成功。你录入的考生信息为:"+ user);
			}else if(12 == menu){
				//修改考生信息
				System.out.println("请输入要修改的考生的准考证号码");
				String searchCard = sc.next();
				
				//? 遍历userList 拿到1个UserEntity的对象user  就可以拿到准考证号码 card 比较 searchCard和card是否相同 
				// 相等 输入新的英语的成绩 english  进行修改
				// 不相等  提示：您输入的考生不存在
				
				
				//1 准备1个计数器
				int c = 0;
				
				//2 遍历userList
				for(int index =0; index<userList.size();index++){
					//3 拿到1个UserEntity的对象user
					UserEntity user = userList.get(index);
					//4 就可以拿到准考证号码 card
					String card = user.getCard();
					
					//5 比较 searchCard和card是否相同
					if(searchCard.equals(card)){
						System.err.println("修改前为:"+ user);
						//6 相等 输入新的英语的成绩 english  进行修改
						System.out.println("请输入要该考生新的英语成绩");
						int english = sc.nextInt();
						user.setEnghish(english);
						System.err.println("=====================修改成绩成功。你修改后的考生信息为:"+ user);
						c = c+1;
						break;
					}
				}
				
				if(c ==0){
					//7 不相等  提示：您输入的考生不存在
					System.out.println("您输入的考生不存在");
					
				}
			}else if(13 == menu){
				//删除考生信息  根据准考证号码来删除考生
				
				System.out.println("请输入要删除的考生的准考证号码");
				String searchCard = sc.next();
				
				
				System.err.println("删除前==========");
				PrintUtil.printList(userList);
				boolean flag =service.deleteUserByCard(searchCard,userList);
				
				if(flag){
					System.err.println("=====================删除成功");
				}else{
					System.err.println("=====================删除失败");
				}
				PrintUtil.printList(userList);
			}else if(14 == menu){
				//查询考生信息 调出新的菜单
				break;
				
			}else if(15 == menu){
				
				//1 打印目前所有的考生信息
				PrintUtil.printList(userList);
				
			}
		}
	}
	
	public static void main(String[] args) {
		
		
		//1 登陆
		UserService service  = new UserService();
		service.login();
		
		
		//2 拿到所有的考生 4
		List<UserEntity> userList = service.getAllUsers();
		
		Scanner sc = new Scanner(System.in);
		int menu = sc.nextInt();
		int counter = 0;
		
		
		//1 进入一级菜单 管理员 
		enterFirstMenu(menu, counter, sc, service,userList);
		
		//2 进入二级菜单
		enterSecondeMenu(service, sc, menu, userList);
		
		//3 进入三级页面
		enterThirdMenu(service, menu, sc, userList);
		
		
		
		
	}

}
